//
//  PywBindVerifyCodeController.m
//  PywSdk
//
//  Created by Jade on 16/8/30.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywBindVerifyCodeController.h"
#import "PywVerifySMSCodeView.h"
#import "PywMasonry.h"
#import "PywBindSetPasswordController.h"
#import "PywMBProgressHUD+Pyw.h"
#import "ApiClientConst.h"
#import "PywUserViewController.h"
#import "PywBuriedManager.h"

@interface PywBindVerifyCodeController ()
@property (nonatomic,strong) PywVerifySMSCodeView *verifySMSCodeView;
@property (nonatomic,copy) NSString *phoneNum;
@property (nonatomic,copy) NSString *smsType;
@property (nonatomic,copy) NSString *status;
@end

@implementation PywBindVerifyCodeController
{
    BOOL _wasKeyBoardManagerEnabled;
}

- (instancetype)initWithStatus:(NSString *)status
                      phoneNum:(NSString *)phoneNum
                         smsType:(NSString *)smsType
{
    self = [super init];
    if (self) {
        self.smsType = smsType;
        self.phoneNum = phoneNum;
        self.status = status;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setUp];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    NSLog(@"%s",__FUNCTION__);
    [PywBuriedManager manager].page_sn = @"5";
    [[PywBuriedManager manager] saveUserActionDataWithPageSn:@"5" actionSn:@"1" typeSn:@"1"];
    _wasKeyBoardManagerEnabled = [[IQKeyboardManager sharedManager] isEnabled];
    [[IQKeyboardManager sharedManager] setEnable:YES];
    [[IQKeyboardManager sharedManager] setEnableAutoToolbar:NO];
    [[IQKeyboardManager sharedManager] setShouldResignOnTouchOutside:YES];
    [[IQKeyboardManager sharedManager] setKeyboardDistanceFromTextField:90];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    NSLog(@"%s",__FUNCTION__);
    [[IQKeyboardManager sharedManager] setEnable:_wasKeyBoardManagerEnabled];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    NSLog(@"%s",__FUNCTION__);
    [PywBuriedManager manager].page_sn = @"";
//    [[PywBuriedManager manager] saveUserActionDataWithPageSn:@"5" actionSn:@"2" typeSn:@"2"];
}
- (void)returnback
{
    [super returnback];
    [[PywBuriedManager manager] saveUserActionDataWithPageSn:@"5" actionSn:@"7" typeSn:@"3"];
}

#pragma mark - setUp
- (void)setUp
{
    self.title = @"验证手机";
    self.navigationItem.rightBarButtonItem = nil;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.verifySMSCodeView];
    [self.verifySMSCodeView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

#pragma mark - getter
- (PywVerifySMSCodeView *)verifySMSCodeView
{
    if (!_verifySMSCodeView) {
        _verifySMSCodeView = [[PywVerifySMSCodeView alloc] initWithStatus:self.status
                                                                 phoneNum:self.phoneNum
                                                                  smsType:self.smsType];
        WEAK_SELF;
        _verifySMSCodeView.verifyCodeComplete = ^(NSString *status,NSString *phoneNum) {
            if ([status integerValue] == 1) {
                [PywMBProgressHUD showSuccess:@"绑定成功!"];
                [weakSelf returnBackAction];
            }else {
                PywBindSetPasswordController *setPasswordView = [[PywBindSetPasswordController alloc] initWithPassport:self.phoneNum];
                [weakSelf.navigationController pushViewController:setPasswordView animated:YES];
            }
            
        };
    }
    return _verifySMSCodeView;
}

#pragma mark - Actions
- (void)returnBackAction
{
    BOOL isPop = NO;
    for (UIViewController *viewController in self.navigationController.viewControllers) {
        if ([viewController isKindOfClass:[PywUserViewController class]]) {
            isPop = YES;
            break;
        }
    }
    
    if (isPop) {
        [[NSNotificationCenter defaultCenter] postNotificationName:ReloadUserCenterData object:nil];
        [self.navigationController popToRootViewControllerAnimated:YES];
    }else {
        [self.navigationController dismissViewControllerAnimated:YES completion:NULL];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
